package in.mvpstarter.sample.common; import android.content.Context; import org.junit.rules.TestRule; import org.junit.runner.Description; import org.junit.runners.model.Statement; import in.mvpstarter.sample.MvpStarterApplication; import in.mvpstarter.sample.common.injection.component.DaggerTestComponent; import in.mvpstarter.sample.common.injection.component.TestComponent; import in.mvpstarter.sample.common.injection.module.ApplicationTestModule; import in.mvpstarter.sample.data.DataManager; /** * Test rule that creates and sets a Dagger TestComponent into the application overriding the * existing application component. * Use this rule in your test case in order for the app to use mock dependencies. * It also exposes some of the dependencies so they can be easily accessed from the tests, e.g. to * stub mocks etc. */ public class TestComponentRule implements TestRule { private final TestComponent mTestComponent; private final Context mContext; public TestComponentRule(Context context) { mContext = context; MvpStarterApplication application = MvpStarterApplication.get(context); mTestComponent = DaggerTestComponent.builder() .applicationTestModule(new ApplicationTestModule(application)) .build(); } public TestComponent getTestComponent() { return mTestComponent; } public Context getContext() { return mContext; } public DataManager getMockDataManager() { return mTestComponent.dataManager(); } @Override public Statement apply(final Statement base, Description description) { return new Statement() { @Override public void evaluate() throws Throwable { MvpStarterApplication application = MvpStarterApplication.get(mContext); application.setComponent(mTestComponent); base.evaluate(); application.setComponent(null); } }; } }